﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using HosPlat.Data;

namespace HosPlat
{
    public class Global : System.Web.HttpApplication
    {
        bool runSocket = false;
        API.Base.BaseManage bm = new API.Base.BaseManage();
        int runCount = 1;

        protected void Application_Start(object sender, EventArgs e)
        {
            //API.Base.BaseManage bm = new API.Base.BaseManage();
            //bm.AUTO();

            System.Timers.Timer t = new System.Timers.Timer();
            t.Interval = 1000;
            t.Elapsed += new System.Timers.ElapsedEventHandler(TimerNow);
            t.AutoReset = true;
            t.Enabled = true;
            t.Start();
        }
        public void TimerNow(object source, System.Timers.ElapsedEventArgs e)
        {

            if (runSocket)
            {
                return;
            }
            string path = Environment.CurrentDirectory;
            runSocket = true;
            try
            {
               
                //读取配置时间
                var cg = new HT_ConfigB().GetEntity(p => p.ID == 1);
                if (cg != null && cg.CallFlag == 1)
                {
                    //API.CommonClass.WriteLog("auto", "auto", runSocket == true ? "1" : "0");
                    int calltime = cg.CallTime.Value;
                    int backtime = cg.BackupTime.Value;
                    int cleartime = cg.ClearTime.Value;

                    //1. 执行获取设备信息
                    if (DateTime.Now.Second % calltime == 0)
                    {
                        bm.AUTO(runCount++);
                        if (runCount == 60)
                        {
                            runCount = 1;
                        }
                       // API.CommonClass.WriteLog("auto", "auto", runCount.ToString());
                    }

                    //2.备份数据
                    if (!cg.BackupTimeLast.HasValue || (cg.BackupTimeLast.HasValue && cg.BackupTimeLast.Value.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd") && (DateTime.Now - cg.BackupTimeLast.Value).Days > (backtime * 7)))
                    {
                        bm.BKDATA(null, null, null, null, DateTime.Now.AddDays(-backtime * 7).ToString("yyyy-MM-dd"), "");
                        //API.CommonClass.WriteLog("auto", "auto", backtime.ToString());
                    }

                    //3.清除历史数据
                    if (!cg.ClearTimeLast.HasValue || (cg.ClearTimeLast.HasValue && cg.ClearTimeLast.Value.ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd") && (DateTime.Now - cg.ClearTimeLast.Value).Days > (cleartime * 7)))
                    {
                        bm.BKDATA2(null, null, null, null, DateTime.Now.AddDays(-cleartime * 7).ToString("yyyy-MM-dd"), "");
                        //API.CommonClass.WriteLog("auto", "auto", cleartime.ToString());
                    }

                }

               

                //UPDATE HT_Layer set IsSync=0 where IsSync=1 and convert(char(10), IsSyncTime,120)!=convert(char(10),getdate(),120) and IP!='' and Port!='' and EquAD!='' 


                // string strUrl = CommonHelp.GetConfig("APITX") + "&r=" + rd.Next();
                //HttpWebResponse ResponseDataXS = CommonHelp.CreateHttpResponse(strUrl, null, 0, "", null, "GET");
                //string Returndata = new StreamReader(ResponseDataXS.GetResponseStream(), Encoding.UTF8).ReadToEnd();

            }
            catch (Exception ex)
            {

                // CommonHelp.WriteLOG(ex.Message.ToString());

            }
            runSocket = false;
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}